package phonebook;

import java.util.List;

import com.andro.chansung.ConstantName;
import com.andro.chansung.ConstantURL;
import com.andro.chansung.DB;
import com.andro.chansung.R;

import android.app.Activity;
import android.app.Fragment;
import android.app.ProgressDialog;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Spinner;

public class PhoneUnivHeadquarterFragment extends Fragment implements OnItemSelectedListener{
	private ListView listView;
	private Spinner spinner;
	private headquarterHandler handler;
	private Activity activity;
	
	private ProgressDialog pd;
	
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		activity = getActivity();
		handler = new headquarterHandler();
		
		SQLiteDatabase db = activity.openOrCreateDatabase(ConstantName.DATABASE_NAME, Context.MODE_WORLD_WRITEABLE, null);
		DB.removePhoneBookTable(db);
		DB.createPhoneTables(db);
		db.close();
		
		View view = inflater.inflate(R.layout.phone_book_browsing_layout, container, false);
		
		listView = (ListView)view.findViewById(R.id.phone_book_list_view);
		spinner  = (Spinner)view.findViewById(R.id.phone_book_spinner);
		
		spinner.setOnItemSelectedListener(this);
		
		PhoneUpdateThread thread = new PhoneUpdateThread(activity, handler, ConstantURL.PHONE_LIST_RETREVING_URL + "?url=headquarter");
		pd = ProgressDialog.show(activity, "로딩중", "대학본부 전화번호 불러오는중...");
		thread.start();
		return view;
	}
	
	private class headquarterHandler extends Handler {
		public void handleMessage(Message msg) {
			if( msg.getData().containsKey("title") ) {
				Log.d("debug", msg.getData().getString("title"));
				SQLiteDatabase db = activity.openOrCreateDatabase(ConstantName.DATABASE_NAME, Context.MODE_WORLD_READABLE, null);
				Phone phone = DB.getDataFromPhoneBookTableByItsHeadName(db, msg.getData().getString("title"));
				db.close();
				
				PhoneListAdapter listAdapter = new PhoneListAdapter(activity, phone);
				listView.setAdapter(listAdapter);
				pd.dismiss();
			}else {
				switch( msg.what ) {
				case 0 :
					SQLiteDatabase db = activity.openOrCreateDatabase(ConstantName.DATABASE_NAME, Context.MODE_WORLD_READABLE, null);
					List<String> lists = DB.getHeadNamesFromPhoneBookTable(db);
					db.close();
					
					ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(activity, android.R.layout.simple_spinner_dropdown_item, lists);
					spinner.setAdapter(spinnerAdapter);
					pd.dismiss();
					break;
				}
			}
		}
	}

	public void onItemSelected(AdapterView<?> parent, View view, 
							  int position, long id) {
		PhoneRetrevingDataThread thread = new PhoneRetrevingDataThread(activity, 
																	   handler, 
																	   ConstantURL.PHONE_DATA_RETREVING_URL + "?url=headquarter",
																	   parent.getItemAtPosition(position).toString());
		pd = ProgressDialog.show(activity, "로딩중", "대학본부 전화번호 불러오는중...");
		thread.start();
	}

	public void onNothingSelected(AdapterView<?> arg0) {}
}
